package com.mlan.userap.untils;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import com.mlan.userap.model.MediaInfo;

import java.io.File;
import java.io.IOException;

public class MediaRecord {

    private MediaRecorder mediaRecorder;
    private static MediaRecord mediaRecordUtil;
    private String saveFile="";
    private MediaPlayer mMediaPlayer;
    private Context context;
    private long startTime; // 记录开始时间




    public MediaRecord(Context ctx) {
        context = ctx;
        init();
    }

    private void init() {
        mediaRecorder = new MediaRecorder();
        //配置采集方式，这里用的是麦克风的采集方式
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //配置输出方式，这里用的是AAC
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
        //配置采样频率，频率越高月接近原始声音，Android所有设备都支持的采样频率为44100
        mediaRecorder.setAudioSamplingRate(44100);
        //配置文件的编码格式,AAC是比较通用的编码格式
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        //配置码率，这里一般通用的是96000
        mediaRecorder.setAudioEncodingBitRate(96000);
        //配置录音文件的位置

        if (Build.VERSION.SDK_INT > 29) {
            saveFile = context.getExternalFilesDir(null).getAbsolutePath() + "/"+System.currentTimeMillis()+".aac";
        } else {
            saveFile = Environment.getExternalStorageDirectory().getPath() + "/"+System.currentTimeMillis()+".aac";
        }

        mediaRecorder.setOutputFile(saveFile);
    }

    /**
     * 开始录制
     */
    public void start() {
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            startTime = System.currentTimeMillis(); // 记录开始时间
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止录制
     */
    public MediaInfo stop() {
        try {
            mediaRecorder.stop();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
        mediaRecorder.reset();   // 重置状态
        mediaRecorder.release(); // 释放资源
        mediaRecorder = null;
        long endTime = System.currentTimeMillis();
        long total = (endTime - startTime)/1000;
        File file = new File(saveFile);
        if (file != null && file.exists()) return  new MediaInfo(saveFile,total);
        return null;
    }

    /**
     * 播放
     * @param path
     * @return
     */
    public boolean play(String path){
        File file = new File(path);
        if(file.exists()){
            Log.e("TAG", "播放: "+path );
            mMediaPlayer = MediaPlayer.create(context,Uri.parse(path));
            mMediaPlayer.setLooping(false);
            mMediaPlayer.start();
            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    Log.e("TAG", "onCompletion: 自动播放完闭" );
                    mMediaPlayer=null;
                }
            });
            return true;
        }
        return false;
    }

    /**
     * 停止播放
     */
    public void stopPlay() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer=null;
        }
    }
}
